<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty TCP粘包与拆包问题深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            border-color: rgba(102, 126, 234, 0.3);
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #e6e6e6;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 2px;
        }
        
        .concept-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            padding: 24px;
            transition: all 0.3s ease;
        }
        
        .concept-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 15px 30px rgba(102, 126, 234, 0.15);
        }
        
        .mermaid {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .first-letter {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-network-wired text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                Netty TCP粘包与拆包
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                深入理解网络通信中的数据边界问题，掌握Netty框架的优雅解决方案
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="first-letter">在</span>Netty中，处理TCP的粘包和拆包问题是网络通信中的一个重要任务。TCP协议本身并不会在应用层区分数据包的边界，这就导致了粘包（多个数据包合并成一个）和拆包（一个数据包被拆分成多个）的问题。
            </p>
        </div>

        <!-- Problem Overview -->
        <section class="mb-16">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-exclamation-triangle mr-3 text-yellow-500"></i>
                粘包和拆包问题概述
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6 mt-12">
                <div class="concept-card">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-gradient-to-br from-purple-500 to-pink-500 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-compress text-white"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">粘包现象</h3>
                    </div>
                    <p class="text-gray-600 leading-relaxed">
                        指的是多个消息被合并在一起发送到接收端，接收端无法正确区分这些消息的边界。这种情况下，原本独立的数据包被TCP协议优化合并，导致应用层无法正确解析。
                    </p>
                </div>
                
                <div class="concept-card">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-gradient-to-br from-blue-500 to-cyan-500 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-expand text-white"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">拆包现象</h3>
                    </div>
                    <p class="text-gray-600 leading-relaxed">
                        指的是一个消息被拆分成多个数据包发送到接收端，接收端需要重新组装这些数据包以还原原始消息。当数据量较大时，TCP会将其拆分成多个片段传输。
                    </p>
                </div>
            </div>
        </section>

        <!-- Visual Diagram -->
        <section class="mb-16">
            <div class="mermaid">
                graph LR
                    A[发送端] -->|消息1+消息2| B[TCP传输]
                    B -->|粘包| C[接收端]
                    
                    D[发送端] -->|大消息| E[TCP传输]
                    E -->|拆包片段1| F[接收端]
                    E -->|拆包片段2| F
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </section>

        <!-- Solutions -->
        <section class="mb-16">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-tools mr-3 text-green-500"></i>
                处理策略
            </h2>
            
            <p class="text-lg text-gray-600 mb-12">
                Netty提供了多种处理策略和工具来解决这些问题，让我们深入了解每种方案的实现细节：
            </p>

            <!-- Solution 1: Fixed Length -->
            <div class="mb-12">
                <div class="card-hover bg-white rounded-xl shadow-lg p-8">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-ruler mr-3 text-indigo-500"></i>
                        定长消息
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        对于每个消息都有固定的长度，可以简单地通过读取指定字节数的数据来解决粘包和拆包问题。Netty提供了<code class="bg-gray-100 px-2 py-1 rounded text-purple-600">FixedLengthFrameDecoder</code>类来实现这一点。
                    </p>
                    
                    <div class="code-block">
                        <div class="code-header">
                            <div class="code-dots">
                                <span class="dot dot-red"></span>
                                <span class="dot dot-yellow"></span>
                                <span class="dot dot-green"></span>
                            </div>
                            <span>FixedLengthFrameDecoderDemo.java</span>
                        </div>
                        <pre><code>import io.netty.handler.codec.FixedLengthFrameDecoder;

public class FixedLengthFrameDecoderDemo {
    public static void main(String[] args) {
        // 定义每个消息的长度为 1024 字节
        int frameLength = 1024;
        FixedLengthFrameDecoder decoder = new FixedLengthFrameDecoder(frameLength);
        
        // 将 decoder 添加到 ChannelPipeline 中
        pipeline.addLast("decoder", decoder);
        pipeline.addLast("handler", new MyHandler());
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Solution 2: Delimiter Based -->
            <div class="mb-12">
                <div class="card-hover bg-white rounded-xl shadow-lg p-8">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-cut mr-3 text-pink-500"></i>
                        分隔符分隔
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        对于使用特定分隔符（如换行符）分隔的消息，可以使用<code class="bg-gray-100 px-2 py-1 rounded text-purple-600">DelimiterBasedFrameDecoder</code>类。这种方法适合于文本协议或以特定字符分隔的消息。
                    </p>
                    
                    <div class="code-block">
                        <div class="code-header">
                            <div class="code-dots">
                                <span class="dot dot-red"></span>
                                <span class="dot dot-yellow"></span>
                                <span class="dot dot-green"></span>
                            </div>
                            <span>DelimiterBasedFrameDecoderDemo.java</span>
                        </div>
                        <pre><code>import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.buffer.Unpooled;

public class DelimiterBasedFrameDecoderDemo {
    public static void main(String[] args) {
        // 定义消息分隔符
        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
        
        // 使用分隔符解码器
        DelimiterBasedFrameDecoder decoder = new DelimiterBasedFrameDecoder(8192, delimiter);
        
        // 将 decoder 添加到 ChannelPipeline 中
        pipeline.addLast("decoder", decoder);
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new MyHandler());
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Solution 3: Length Field -->
            <div class="mb-12">
                <div class="card-hover bg-white rounded-xl shadow-lg p-8">
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-hashtag mr-3 text-green-500"></i>
                        长度字段
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        如果消息中包含长度字段（例如，前4个字节表示消息的长度），可以使